package UI;

import Entity.ExamInfo;
import Entity.Question;
import Entity.QuestionInfo;
import Entity.User;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Exam extends JFrame {
    private JLabel questionCount;//题目数量
    private JLabel timer;//剩余时间
    private JButton prev;//上一题
    private JButton next;//下一题
    private JLabel examInfo;//info是私有成员变量
    private JTextArea questionArea;//多行文本域
    private ChangeUI changeUI;

    public void setChangeUI(ChangeUI changeUI) {
        this.changeUI = changeUI;
    }

    //无参构造方法
    public Exam() {
        init();//创建对象时,调用页面初始化方法
    }
    /**1.初始化界面组件和布局的 */
    private void init() {
        setTitle("指针信息在线测评");
        setSize(600, 380);
        //2.在初始化界面中添加创建内容窗格方法
        setContentPane(createContentPane());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int i = JOptionPane.showConfirmDialog(null, "退出");
                if (i==JOptionPane.YES_OPTION){
                    System.exit(0);
                }
            }
        });
    }

    /**2.创建内容窗格*/
    private JPanel createContentPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.setBorder(new EmptyBorder(6, 6, 6, 6));
        //加载图片资源
        ImageIcon icon = new ImageIcon(getClass().getClassLoader().
                getResource("pic/exam_title.png"));
        pane.add(BorderLayout.NORTH, new JLabel(icon));
        pane.add(BorderLayout.CENTER, createCenterPane());
        pane.add(BorderLayout.SOUTH, createToolsPane());
        return pane;
    }
    //题目的题号和 倒计时时间都是会变的,所以要改成成员变量
    //private JLabel questionCount;//题目数量
    //private JLabel timer;//剩余时间
    private JPanel createToolsPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.setBorder(new EmptyBorder(0, 10, 0, 10));

        /*JLabel */questionCount = new JLabel("题目:20 的 1题");
        /*JLabel */timer = new JLabel("剩余时间:222秒");
        pane.add(BorderLayout.WEST, questionCount);
        pane.add(BorderLayout.EAST, timer);
        //4.2添加3个按钮
        pane.add(BorderLayout.CENTER, createBtnPane());
        return pane;
    }

    //设置3个按钮
   /* private JButton prev;//私有成员变量
    private JButton next;//私有成员变量*/
    private JPanel createBtnPane() {
        JPanel pane = new JPanel();
        /*这里的上一题/下一题按了以后都是要切换题目的,
         * 所以以后也是要修改成成员变量使用*/
        prev = new JButton("上一题");
        next = new JButton("下一题");
        JButton send = new JButton("交卷");
        pane.add(prev);
        pane.add(next);
        pane.add(send);
        prev.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                changeUI.prev();
            }
        });

        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                changeUI.next();
            }
        });

        send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                changeUI.send();
            }
        });
        return pane;
    }


    /**3.设置中部布局 - 分为三部分:
     上:添加文本标签,居中显示
     中:添加试题滚动区域
     下:添加多选按钮
     */
    /*private JLabel examInfo;//info是私有成员变量*/
    private JPanel createCenterPane() {
        JPanel pane = new JPanel(new BorderLayout());
        //添加文本标签,居中显示
        examInfo = new JLabel
                ("姓名:XXX 考试:XXX 考试时间:XXX", JLabel.CENTER);
        //info = examInfo;这个是后期需要使用,进行修改信息时使用
        pane.add(BorderLayout.NORTH, examInfo);
        //添加试题滚动区域
        pane.add(BorderLayout.CENTER, createQuestionPane());
        //添加多选按钮
        pane.add(BorderLayout.SOUTH, createOptionsPane());
        return pane;
    }

    //3.1 设置试题滚动区域
    /* private JTextArea questionArea;//多行文本域*/
    private /*JPanel*/ JScrollPane createQuestionPane() {

        /*JScrollPane:滚动画板
            作用:做试题的滚动换题*/
        JScrollPane pane = new JScrollPane();
        //设置标题框
        pane.setBorder(new TitledBorder("题目"));
        /*导入试题*/
        questionArea = new JTextArea();
        questionArea.setText("问题\nA.\nB.");
        questionArea.setLineWrap(true);// 允许折行显示(换行)
        questionArea.setEditable(false);// 不能够编辑内容
        // JTextArea 必须放到 JScrollPane 的视图区域中(Viewport)
        pane.getViewport().add(questionArea);
        return pane;
    }

    //设置选择按钮
    //添加一个自定义的选项集合, 方便答案读取的处理
    private OptionBox[] options = new OptionBox[4];
    private JPanel createOptionsPane() {
        JPanel pane = new JPanel();
        OptionBox a = new OptionBox(0, "A");
        OptionBox b = new OptionBox(1, "B");
        OptionBox c = new OptionBox(2, "C");
        OptionBox d = new OptionBox(3, "D");
        //为了方便读取答案,设置界面时,可以先不要
        options[0] = a;
        options[1] = b;
        options[2] = c;
        options[3] = d;
        pane.add(a);
        pane.add(b);
        pane.add(c);
        pane.add(d);
        return pane;
    }

    public void updateView(ExamInfo examInfo, QuestionInfo questionInfo) {
        questionArea.setText(questionInfo.toString());
        updateOptions(questionInfo.getUserAnswers());
        updateButtons(examInfo.getQuestionCount(), questionInfo.getQuestionIndex());
        updateQuestionNumber(examInfo.getQuestionCount(), questionInfo.getQuestionIndex());
    }

    public void updateUser(String name) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy - MM - dd");
        Date date = new Date();
        String format = dateFormat.format(date);
        examInfo.setText("姓名:"+name+"考试:综合测试 考试时间:"+format);
    }

    private void updateQuestionNumber(int questionCount, int questionIndex) {
        String str = "题目:" + questionCount + " 的 " + (questionIndex + 1) + "题";
        this.questionCount.setText(str);
    }

    private void updateButtons(int questionCount, int questionIndex) {
        prev.setEnabled(questionIndex != 0);
        next.setEnabled((questionCount - 1) != questionIndex);
    }

    private void updateOptions(List<Integer> userAnswers) {
        for (OptionBox option : options) {
            option.setSelected(userAnswers.contains(option.value));
        }
    }

    public List<Integer> getUserAnswers() {
        List<Integer> list = new ArrayList<Integer>();
        for (OptionBox option : options) {
            if (option.isSelected()) {
                list.add(option.value);
            }
        }
        return list;
    }

    public void showTime(long h, long m, long s) {
        String t = "剩余时间:" + h + ":" + m + ":" + s;
        if (h == 0 && m < 5) {
            timer.setForeground(Color.red);
        }
        timer.setText(t);
    }

    /*自定义多选(复选)选项: JCheckBox
     *   使用内部类扩展了 JCheckBox 增加了val属性,代表答案值
     */
    private class OptionBox extends JCheckBox{
        //为了保证Option序列化不变
        private static final long serialVersionUID = 5569918347028393191L;
        //定义一个value值,代表答案选项
        int value;
        //添加有参构造方法
        public  OptionBox(int val, String txt) {
            super(txt);//调用父类JCheckBox(String txt)
            this.value = val;
        }
    }
}
